package cn.sc.allinpay.allfavour.module.printer;

import android.text.TextUtils;

import com.landicorp.android.eptapi.device.Printer;
import com.landicorp.android.eptapi.exception.RequestException;

/**
 * 打印基类
 * author：pany
 * on 2017/11/3 11:44
 */
public abstract class BasePrinter implements IPrinter {

    private int step = 1;//联数
    private PrintData data;
    private OnPrintListener listener;

    public BasePrinter(PrintData data) {
        this.data = data;
    }

    /**
     * 打印
     */
    private Printer.Progress progress = new Printer.Progress() {

        @Override
        public void doPrint(Printer printer) {
            // TODO Auto-generated method stub
            listener.onStart(step);
            try {
                printLogo(printer, data);
                printHeader(printer, data);
                printBody(printer, data);
                printFooter(printer, data);
            } catch (Exception e) {
                listener.onFail(-1, "打印异常" + e.getMessage());
            }
        }

        @Override
        public void onFinish(int code) {
            // TODO Auto-generated method stub
            if (Printer.ERROR_NONE == code) {//打印正常
                listener.onSuccess(step);
                step++;
            } else {
                listener.onFail(code, getPrinterError(code));
            }
        }

        @Override
        public void onCrash() {
            // TODO Auto-generated method stub
            listener.onFail(-1, "打印机崩溃");
        }
    };


    @Override
    public void print(OnPrintListener listener) {
        this.listener = listener;
        try {
            progress.start();
        } catch (RequestException e) {
            listener.onFail(-1, "开始打印异常" + e.getMessage());
        }
    }

    /**
     * 打印标志
     */
    protected void printLogo(Printer printer, PrintData data) throws Exception {
        printer.setFormat(FormatStyle.createLargeFont());

        if (!TextUtils.isEmpty(data.getPrintCount())) {

            printer.println("        通联支付--(重复)");
        } else {
            printer.println("        通联支付");
        }
    }

    /**
     * 打印头部
     */
    protected void printHeader(Printer printer, PrintData data) throws Exception {
        printer.setFormat(FormatStyle.createNormalFont());
        printer.println("商户名:" + data.getMerchantName());
        printer.println("终端号:" + data.getTerminalId());
        printer.println("收银员号:" + data.getOperNo());
        printer.println("收银员名:" + data.getOperName());
        if (data.getOilName() != null) {
            printer.println("油品号:" + data.getOilName());
        }
    }

    /**
     * 打印内容
     */
    protected abstract void printBody(Printer printer, PrintData data) throws Exception;

    /**
     * 打印结尾
     */
    protected void printFooter(Printer printer, PrintData data) throws Exception {

    }

    /**
     * 获取打印机错误描述
     *
     * @param code 错误码
     * @return 错误描述
     */
    private String getPrinterError(int code) {
        switch (code) {
            case Printer.ERROR_PAPERENDED:
                return "缺纸，不能打印";
            case Printer.ERROR_HARDERR:
                return "硬件错误";
            case Printer.ERROR_OVERHEAT:
                return "打印头过热";
            case Printer.ERROR_BUFOVERFLOW:
                return "操作的位置超出范围";
            case Printer.ERROR_LOWVOL:
                return "低压保护";
            case Printer.ERROR_PAPERENDING:
                return "纸张将要用尽，还允许打印";
            case Printer.ERROR_MOTORERR:
                return "打印机芯故障(过快或者过慢)";
            case Printer.ERROR_PENOFOUND:
                return "自动定位没有找到对齐位置,纸张回到原来位置";
            case Printer.ERROR_PAPERJAM:
                return "卡纸";
            case Printer.ERROR_NOBM:
                return "没有找到黑标";
            case Printer.ERROR_BUSY:
                return "打印机处于忙状态";
            case Printer.ERROR_BMBLACK:
                return "黑标探测器检测到黑色信号";
            case Printer.ERROR_WORKON:
                return "打印机电源处于打开状态";
            case Printer.ERROR_LIFTHEAD:
                return "打印头抬起";
            case Printer.ERROR_LOWTEMP:
                return "低温保护或AD出错";
        }
        return "未知错误 (" + code + ")";
    }
}
